import { Component, OnInit } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd/message';
import { Router } from '@angular/router';
import { LoginModel } from './login.model';
import { take, timer } from 'rxjs';
@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.less'],
})
export class LoginComponent implements OnInit {
  isOkLoading = false;
  passwordVisible = false;
  password: string = '';
  username: string = '';
  constructor(public msg: NzMessageService, private router: Router) {}

  ngOnInit(): void {}

  login() {
    if (this.isOkLoading) return;
    this.isOkLoading = true;
    const data = new LoginModel(this.username, this.password);
    console.log(data);
    this.msg.loading('登录中...');
    // 使用rxjs模拟延迟1秒的请求
    timer(1000)
      .pipe(take(1))
      .subscribe(() => {
        if (data.username === 'admin' && data.password === '123456') {
          this.msg.info('登录成功');
          this.router.navigate(['layout']);
        } else {
          this.msg.remove();
          this.msg.error('登录失败');
        }
        this.isOkLoading = false;
      });
  }
}
